package com.vclee.fast.integration.imserver;


import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vclee.fast.utils.model.Bson;


/**
 * Author: sir.Li
 * email: lesli2@qq.com
 * Date: 2021/5/14
 */
public class ImDataProcessor {

    private static final Logger logger = LoggerFactory.getLogger(ImDataProcessor.class);

    static void handImData(Channel channel, String content) {
        StringBuilder builder = new StringBuilder();
        XunFeiService.conversation(100L, 100L, content, response -> {
            Bson data = Bson.create(response);
            if(data.getBoolean("finish")){
                channel.writeAndFlush(new PingWebSocketFrame());
                channel.writeAndFlush(new TextWebSocketFrame(builder.toString()));
            }else{
                builder.append(data.getString("content"));
            }
        });
    }

    private static String getResponse(String content) {
        Bson json = Bson.create(content);
        String msgId = json.getString("msgId"); // 前缀+ID号+时间戳
        Bson args = json.getBson("args");
        Bson response = new Bson();
        response.put("msgId", msgId);
        response.put("args", args); // 请求参数
        return response.toString();
    }
}
